// 定义一个基础类
class Animal {
  void eat() {
    print("Animal is eating...");
  }
}

// 定义两个 mixin（混入）
// mixin 可以看作是一组“功能模块”，可以被多个类复用
mixin Flyer {
  void fly() {
    print("Flying in the sky 🕊️");
  }
}

mixin Swimmer {
  void swim() {
    print("Swimming in the water 🐬");
  }
}

// 定义一个类继承 Animal，同时混入两个功能模块
class Duck extends Animal with Flyer, Swimmer {
  void quack() {
    print("Quack! 🦆");
  }

  // 重写父类方法
  @override
  void eat() {
    print("Duck is eating grains...");
  }
}

// 另一个类演示 mixin 冲突时的顺序规则
mixin Walker {
  void move() => print("Walking on land 🚶");
}

mixin Runner {
  void move() => print("Running fast 🏃");
}

// Dog 类混入了两个含有相同方法的 mixin
class Dog with Walker, Runner {
  // mixin 的方法调用顺序是“从左到右，后者覆盖前者”
  void bark() => print("Woof! 🐶");
}

void main() {
  var duck = Duck();
  duck.eat(); // Duck is eating grains...
  duck.fly(); // Flying in the sky 🕊️
  duck.swim(); // Swimming in the water 🐬
  duck.quack();

  print("------");

  var dog = Dog();
  dog.move(); // Running fast 🏃（Runner 覆盖了 Walker）
  dog.bark();
}
